import { ElMessage, ElMessageBox } from 'element-plus'
import { h } from 'vue'

export function useMessage() {
  const showMessage = (message: string, type: 'success' | 'warning' | 'info' | 'error' = 'info') => {
    // 如果消息包含换行符，使用 MessageBox 显示
    if (message.includes('\n')) {
      const lines = message.split('\n')
      ElMessageBox({
        title: type === 'error' ? '错误信息' : '提示',
        message: h('div', null, lines.map(line => h('p', { style: 'margin: 5px 0' }, line))),
        type: type,
        confirmButtonText: '确定',
        showCancelButton: false,
        dangerouslyUseHTMLString: false
      })
    } else {
      ElMessage({
        message,
        type,
        duration: 3000
      })
    }
  }

  const showSuccess = (message: string) => {
    showMessage(message, 'success')
  }

  const showWarning = (message: string) => {
    showMessage(message, 'warning')
  }

  const showError = (message: string) => {
    showMessage(message, 'error')
  }

  const showInfo = (message: string) => {
    showMessage(message, 'info')
  }

  return {
    showMessage,
    showSuccess,
    showWarning,
    showError,
    showInfo
  }
}